package com.example.demo.service.impl;

import com.example.demo.dao.SongMapper;
import com.example.demo.model.Song;
import com.example.demo.service.SongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SongServiceImpl implements SongService {

  @Autowired
  private SongMapper songMapper;

  @Override
  public void save(Song song) {
    songMapper.insertSelective(song);
  }

  @Override
  public void update(Song song) {
    songMapper.updateByPrimaryKeySelective(song);
  }

  @Override
  public void delete(Integer id) {
    songMapper.deleteByPrimaryKey(id);
  }

  @Override
  public Song getDetail(Integer id) {
    return songMapper.selectByPrimaryKey(id);
  }

  @Override
  public Object getList(String name, Integer currentPage, Integer limit) {
    Map<String, Object> map = new HashMap<>();
    int offset = (currentPage - 1) * limit;
    List<Song> productList = songMapper.getListByParams(name, limit, offset);
    int totalCount = songMapper.countByParams(name);
    map.put("songList", productList);
    map.put("totalCount", totalCount);
    return map;
  }
}
